package com.carpedil.shirodemo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.carpedil.shirodemo.common.util.ResultVO;
import com.carpedil.shirodemo.common.validate.Create;
import com.carpedil.shirodemo.common.validate.Update;
import com.carpedil.shirodemo.system.model.User;
import com.carpedil.shirodemo.system.service.IRoleService;
import com.carpedil.shirodemo.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表 前端控制器
 *
 * @author carpedil
 * @since 2021-01-05
 */
@Controller
@RequestMapping("/user")
public class UserController {
  @Autowired IUserService userService;
  @Autowired IRoleService roleService;

  @GetMapping("/index")
  public String index() {
    return "/user/user-list";
  }

  @GetMapping
  public String toAdd(Model model) {
    model.addAttribute("roles", roleService.selectAll());
    return "user/user-add";
  }

  @PostMapping
  @ResponseBody
  public ResultVO addUser(
      @Validated(Create.class) User user,
      @RequestParam(value = "role[]", required = false) Integer[] roleIds) {
    System.out.println("role = " + roleIds);
    boolean save = userService.save(user, roleIds);
    if (save) {
      return ResultVO.success();
    } else {
      return ResultVO.fail(500, "添加失败");
    }
  }

  @GetMapping("/{id}")
  public String toUpdate(@PathVariable("id") Integer id, Model model) {
    System.out.println("userService.getById(id) = " + userService.getById(id));
    model.addAttribute("user", userService.getById(id));
    model.addAttribute("roles", roleService.selectAll());
    return "user/user-add";
  }

  @DeleteMapping("/{id}")
  @ResponseBody
  public ResultVO delete(@PathVariable("id") Integer id) {
    boolean b = userService.deleteUserById(id);
    if (b) {
      return ResultVO.success();
    } else {
      return ResultVO.fail(500, "删除失败");
    }
  }

  @PutMapping
  @ResponseBody
  public ResultVO updateUser(
      @Validated(Update.class) User user,
      @RequestParam(value = "role[]", required = false) Integer[] roleIds) {
    boolean flag = userService.updateUser(user, roleIds);
    if (flag) {
      return ResultVO.success();
    } else {
      return ResultVO.fail(500, "修改失败");
    }
  }

  @PostMapping("/{userId}/enable")
  @ResponseBody
  public ResultVO enableUser(@PathVariable("userId") Integer userId) {
    System.out.println("userId = " + userId);
    boolean b = userService.enable(userId);
    if (b) {
      return ResultVO.success();
    } else {
      return ResultVO.fail(500, "激活失败");
    }
  }

  @PostMapping("/{userId}/disable")
  @ResponseBody
  public ResultVO disableUser(@PathVariable("userId") Integer userId) {
    System.out.println("userId = " + userId);
    boolean b = userService.disable(userId);
    if (b) {
      return ResultVO.success();
    } else {
      return ResultVO.fail(500, "激活失败");
    }
  }

  @GetMapping("/list")
  @ResponseBody
  public ResultVO getList(Integer page, Integer limit, User userQuery) {
    IPage<User> userPage = userService.selectAllWithDept(page, limit, userQuery);
    return ResultVO.success(userPage);
  }
}
